home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 13316 / 13316.xpi / content / gridViewDrawing.js < prev    next >
Encoding:
JavaScript  |  2009-07-27  |  13.6 KB  |  445 lines

  1.  
  2. /* Copyright (C) 2009 Norman Solomon
  3.  * e-mail: historytree.addon@yahoo.com
  4.  * 
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the License.
  12.  */
  13.  
  14. // ********************************************************************
  15. // *****                                                          *****
  16. // *****    GRID-VIEW IMAGE DISPLAY AND MOUSE-CLICK PROCESSING    *****
  17. // *****    --------------------------------------------------    *****
  18. // *****      Displaying GridView ".jpeg" images on gCanvas       *****
  19. // *****    and handling user mouse-clicks on GridView gCanvas    *****
  20. // *****                                                          *****
  21. // ********************************************************************
  22.  
  23. // ==================================================================
  24. // Draws the gNodeList[] images stored in all HistoryNode objects
  25. // ==================================================================
  26. function drawAllImagesOnCanvas(numAcross, scrollToTop)
  27. {
  28.     // Init integer vars 
  29.     var availWidth, imgWidth, imgHeight;
  30.     var numVisible, reqRows, reqHgt;
  31.     var imgX, imgY;
  32.     var ndx = -1;
  33.     var drawDelay;
  34.  
  35.     // Init other vars
  36.     var tabID;             // FF Tab tabID
  37.     var showOpenPage;     // Boolean
  38.     var tabRoot, tNode;     // TreeNode's
  39.     var hNode;             // HistoryNode
  40.     var optGrpView;         // <radiogroup...>
  41.     var noImgsMsg;         // String
  42.  
  43.     // ----------------------------------------------------
  44.     // Stop GV display setInterval() if its already running
  45.     if (gGVdispInterval !== null)
  46.     {
  47.         clearInterval(gGVdispInterval);
  48.         gGVdispInterval = null;
  49.     }
  50.  
  51.     // Show message and exit if no GridView images to show
  52.     numVisible = numVisibleTreeNodes();
  53.     if (numVisible === 0)
  54.     {
  55.         // Message to show depends on quick-view option
  56.         optGrpView = document.getElementById("optGrpView");
  57.         if (optGrpView.selectedIndex === 3 
  58.          && numExtWinOpenTabs() === gTabRoots.length)
  59.             noImgsMsg = "No Tabs have been closed";
  60.         else
  61.             noImgsMsg = "History tree is fully contracted (no images to show)";
  62.  
  63.         // Show big white message, then exit
  64.         showBigMessageOnEmptyCanvas(noImgsMsg, "bold 14pt verdana");
  65.         return;
  66.     }
  67.  
  68.     // ----------------------------------------------------
  69.     // Set width and height for all drawn GridView images
  70.     gCanvas.width = CANVAS_MIN_WID;      // Always for GridView
  71.     availWidth = gCanvas.width - (GV_HGAP * (numAcross + 1));    
  72.     imgWidth = Math.round(availWidth / numAcross);
  73.     imgHeight = Math.round(imgWidth * WH_RATIO);
  74.  
  75.     // Set gCanvas.height required to draw all GridView images
  76.     reqRows = Math.ceil(numVisible / numAcross); 
  77.     reqHgt = reqRows * (imgHeight + GV_VGAP) + 30;
  78.     if (reqHgt < CANVAS_MIN_HGT)
  79.         gCanvas.height = CANVAS_MIN_HGT;
  80.     else
  81.         gCanvas.height = reqHgt;
  82.  
  83.     // Scroll to top of gCanvas if param passed
  84.     if (scrollToTop)
  85.         gCanvasScroller.scrollTo(0, 0);
  86.  
  87.     // Clear the canvas before drawing images
  88.     gCtx.fillStyle = CANVAS_BAK_COL;  // Dark grey
  89.     gCtx.fillRect(0, 0, gCanvas.width, gCanvas.height);
  90.  
  91.     // -----------------------------------------------------------
  92.     // Init global array that stores GV display TreeNodes
  93.     gGVdispArray = new Array();
  94.  
  95.     // Draw web-page images for all TreeNode's visible in TreeView
  96.     for (var i = 0; i < gTabRoots.length; i++)
  97.     {
  98.         // Only draw images for Tabs that are visible in TreeView
  99.         tabRoot = gTabRoots[i];
  100.         if (!tabRoot.hidden)
  101.         {
  102.             // Draw HistoryNode web-page images in the order they were added to 
  103.             // gNodeList[] for this tabID - open pages first, then closed pages
  104.             tabID = tabRoot.histNode.tab;
  105.             for (var tf = 0; tf < 2; tf++)
  106.             {
  107.                 // Set flag used in for(j) loop, below
  108.                 if (tf === 0)
  109.                     showOpenPage = true;
  110.                 else
  111.                     showOpenPage = false;
  112.                 
  113.                 // Two passes down gNodeList[] are made for each tabID
  114.                 for (var j = 0; j < gNodeList.length; j++)
  115.                 {
  116.                     // Only draw images for visible TreeNodes in this tab
  117.                     hNode = gNodeList[j];
  118.                     tNode = gTreeNodes[hNode.treeNodeNdx + 1]; // + 1 for added Root
  119.                     if (hNode.tab === tabID && !tNode.hidden 
  120.                      && tNode.isOpenPage === showOpenPage)
  121.                     {
  122.                         // Set (imgX, imgY) used to draw image on gCanvas
  123.                         ndx ++;
  124.                         if (ndx === 0)
  125.                         {
  126.                             // First image is always drawn at top-left corner
  127.                             imgX = GV_HGAP;
  128.                             imgY = GV_HGAP;
  129.                         }
  130.                         else
  131.                         {
  132.                             // Move down gCanvas if have reached end of a row
  133.                             if (Math.floor(ndx / numAcross) - (ndx / numAcross) === 0)
  134.                             {
  135.                                 imgX = GV_HGAP;
  136.                                 imgY += imgHeight + GV_VGAP;
  137.                             }
  138.                             else
  139.                             {
  140.                                 // Still on same row - So just move across gCanvas
  141.                                 imgX += imgWidth + GV_HGAP;        
  142.                             }
  143.                         }
  144.  
  145.                         // Set props used to draw image and detect mouse-clicks on it
  146.                         tNode.gridViewImgX = imgX - 1;
  147.                         tNode.gridViewImgY = imgY - 1;
  148.                         tNode.gridViewImgWid = imgWidth + 2;
  149.                         tNode.gridViewTabNum = i;
  150.                         
  151.                         // Add TreeNode ref to end of global array
  152.                         gGVdispArray.push(tNode);
  153.                     }
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     
  159.     // ---------------------------------------------------
  160.     // Start setInterval() that draws GV images
  161.     drawDelay = Math.round(imgWidth / 30);
  162.     gGVdispInterval = setInterval(drawGridView, drawDelay);
  163. }
  164.  
  165. // ====================================================
  166. // Draws GridView using setInterval() and global array
  167. // ====================================================
  168. function drawGridView()
  169. {
  170.     // Draw image and desc then remove from global array
  171.     if (gGVdispArray.length > 0)
  172.     {
  173.         // Get TreeNode reference
  174.         tNode = gGVdispArray[0];
  175.  
  176.         // Draw the GridView image and description
  177.         drawGridViewImage(tNode, true);
  178.         drawGridViewDescription(tNode, true);
  179.             
  180.         // Remove TreeNode reference from array
  181.         gGVdispArray.splice(0,1);
  182.     }
  183.     else
  184.     {
  185.         // Stop setInterval() and set global flag
  186.         clearInterval(gGVdispInterval);
  187.         gGVdispInterval = null;
  188.     }
  189. }
  190.  
  191. // ===========================================================
  192. // Draws GridView ".jpeg" image on gCanvas using passed tNode
  193. // ===========================================================
  194. function drawGridViewImage(tNode, showHighlight)
  195. {
  196.     // Init vars used to draw image
  197.     var x, y, w, h;      // Integers
  198.     var hNode;          // HistoryNode
  199.     var bordCol;      // Color
  200.     var roundRect;      // Boolean
  201.     
  202.     // ---------------------------------------------------
  203.     // Get image co-ordinates from tNode
  204.     x = tNode.gridViewImgX + 1;
  205.     y = tNode.gridViewImgY + 1;
  206.     w = tNode.gridViewImgWid - 2;
  207.     h = Math.round(w * WH_RATIO);
  208.  
  209.     // Get ref to corresponding gNodeList[] HistoryNode
  210.     hNode = tNode.histNode;
  211.  
  212.     // Set ".jpeg" image border shape and colour
  213.     if (hNode === gParamArray[1] 
  214.     || (showHighlight && isHighlightedNode(tNode)))
  215.     {
  216.         // Current FF page or node found via Find Next/Prev
  217.         bordCol = "red";
  218.         roundRect = true;
  219.     }
  220.     else if (tNode.parent === null || !tNode.isOpenPage)
  221.     {
  222.         // "Dummy" root or node representing closed FF page
  223.         bordCol = "rgb(0,55,219)";    // Blue
  224.         roundRect = false;
  225.     }
  226.     else
  227.     {
  228.         // Open FF page (not root and not current FF page)
  229.         bordCol = "black";
  230.         roundRect = false;
  231.     }
  232.     
  233.     // Clear any previous border highlight
  234.     gCtx.lineWidth = 3;
  235.     gCtx.strokeStyle = CANVAS_BAK_COL;
  236.     gCtx.strokeRect(x - 3, y - 3, w + 6, h + 6);
  237.  
  238.     // Draw hNode ".jpeg" image or a white bordered rectangle
  239.     if (!blankRectangleDrawn
  240.         (hNode, x, y, w, h, roundRect, bordCol))
  241.     {
  242.         // Page HAS appeared in an FF Tab - So draw ".jpeg" image
  243.         if (!hNode.imgCreated)
  244.         {
  245.             // Slow draw - once only
  246.             drawAndUpdateNodeImage(hNode, x, y, w, h);
  247.         }
  248.         else
  249.         {
  250.             // Fast draw - all further draws
  251.             gCtx.drawImage(hNode.imgToDraw, x, y, w, h);
  252.         }
  253.  
  254.         // Draw overlapping border that contains the image
  255.         if (roundRect)
  256.         {
  257.             // Rounded corner rect with highlighted border
  258.             gCtx.lineWidth = 3;
  259.             drawRoundedRect
  260.                 (x - 2, y - 2, w + 4, h + 4, 10, bordCol, false);
  261.         }
  262.         else
  263.         {
  264.             // Square rect with black border
  265.             gCtx.lineWidth = 2;
  266.             gCtx.strokeStyle = bordCol;
  267.             gCtx.strokeRect(x - 1, y - 1, w + 2, h + 2);
  268.         }
  269.     }
  270. }
  271.  
  272. // ============================================================
  273. // Draws GV web-page desc and info for passed tNode on gCanvas
  274. // Drawn text will be highlighted or cleared of highlighting
  275. // ============================================================
  276. function drawGridViewDescription(tNode, showHighlight)
  277. {
  278.     // Init integers
  279.     var x, y, w, h, tabNum;
  280.     var descWid, timeWid, infoWid;
  281.     var rectX, textX;
  282.  
  283.     // Init other vars
  284.     var truncDesc, timeInfo, pageInfo;    // Strings
  285.     var hNode;                            // HistoryNode
  286.     var tabRoot;                        // TreeNode
  287.  
  288.     // ------------------------------------------------
  289.     // Get drawn image co-ordinates from passed tNode
  290.     x = tNode.gridViewImgX;
  291.     y = tNode.gridViewImgY;
  292.     w = tNode.gridViewImgWid;
  293.     h = Math.round(w * WH_RATIO);
  294.  
  295.     // Get ref to corresponding gNodeList[] HistoryNode
  296.     hNode = tNode.histNode;
  297.     
  298.     // Get Tab number for tNode - As shown in <listbox...>
  299.     tabNum = tNode.gridViewTabNum;
  300.  
  301.     // ---------------------------------------------
  302.     // Get width of truncated web-page description
  303.     gCtx.mozTextStyle = "bold 8pt verdana";
  304.     truncDesc = truncatedText(hNode.desc, w - 9);
  305.     descWid = gCtx.mozMeasureText(truncDesc);
  306.  
  307.     // Get widths of timeInfo and page-info strings
  308.     gCtx.mozTextStyle = "7pt verdana";
  309.     timeInfo = hNode.timeInfo;
  310.     timeWid = gCtx.mozMeasureText(timeInfo);
  311.  
  312.     if (hNode === gParamArray[1])
  313.         pageInfo = "  CURRENT";
  314.     else if (tNode.isOpenPage)
  315.         pageInfo = "  Open";
  316.     else
  317.         pageInfo = "  Closed";
  318.  
  319.     pageInfo += " (Tab " + (tabNum + 1).toString() + ")";
  320.     infoWid = timeWid + gCtx.mozMeasureText(pageInfo);
  321.  
  322.     // ----------------------------------------------------------
  323.     // Draw two lines of highlighted/cleared text below GV image
  324.     if (showHighlight && isHighlightedNode(tNode))
  325.     {
  326.         // Draw light-green background highlighting rectangle
  327.         gCtx.fillStyle = "rgb(184,225,174)";
  328.         if (descWid > infoWid)
  329.         {
  330.             rectX = Math.round((x + (w - descWid) / 2) - 5);
  331.             gCtx.fillRect(rectX, (y + h + 4), descWid + 10, 34);
  332.         }
  333.         else
  334.         {
  335.             rectX = Math.round((x + (w - infoWid) / 2) - 5);
  336.             gCtx.fillRect(rectX, (y + h + 4), infoWid + 10, 34);
  337.         }
  338.  
  339.         // Draw red page description
  340.         textX = Math.round(x + (w - descWid) / 2);
  341.         gCtx.fillStyle = "rgb(160,0,0)";
  342.         gCtx.mozTextStyle = "bold 8pt verdana";
  343.         fillText(truncDesc, textX, (y + h + 17));
  344.  
  345.         // Draw time page loaded below web-page description
  346.         textX = Math.round(x + (w - infoWid) / 2);
  347.         gCtx.fillStyle = "rgb(0,0,128)";  // Dark blue
  348.         gCtx.mozTextStyle = "7pt verdana";
  349.         fillText(timeInfo, textX, (y + h + 32));
  350.  
  351.         // Draw page-info to right of time string
  352.         gCtx.fillStyle = "black";
  353.         fillText(pageInfo, textX + timeWid, (y + h + 32));
  354.     }
  355.     else
  356.     {
  357.         // Draw gCanvas background colour clearing rectangle
  358.         gCtx.fillStyle = CANVAS_BAK_COL;
  359.         if (descWid > infoWid)
  360.         {
  361.             rectX = Math.round((x + (w - descWid) / 2) - 6);
  362.             gCtx.fillRect(rectX, (y + h + 4), descWid + 12, 34);
  363.         }
  364.         else
  365.         {
  366.             rectX = Math.round((x + (w - infoWid) / 2) - 6);
  367.             gCtx.fillRect(rectX, (y + h + 4), infoWid + 12, 34);
  368.         }
  369.         
  370.         // Set page description text color
  371.         if (hNode === gParamArray[1])
  372.             gCtx.fillStyle = "white";
  373.         else if (!tNode.isOpenPage)
  374.             gCtx.fillStyle = "rgb(180,190,255)";  // Blue
  375.         else if (hNode.tab === gCurrentTabID)
  376.             gCtx.fillStyle = "rgb(255,172,117)";  // Orange
  377.         else
  378.             gCtx.fillStyle = "rgb(255,255,128)";  // Yellow
  379.  
  380.         // Draw page description
  381.         textX = Math.round(x + (w - descWid) / 2);
  382.         gCtx.mozTextStyle = "bold 8pt verdana";
  383.         fillText(truncDesc, textX, (y + h + 17));
  384.  
  385.         // Draw time page loaded below page description
  386.         textX = Math.round(x + (w - infoWid) / 2);
  387.         gCtx.mozTextStyle = "7pt verdana";
  388.         gCtx.fillStyle = "rgb(204,255,212)";    // Light green
  389.         fillText(timeInfo, textX, (y + h + 32));
  390.  
  391.         // Draw page-info to right of time string
  392.         gCtx.fillStyle = "white";
  393.         fillText(pageInfo, textX + timeWid, (y + h + 32));
  394.     }
  395. }
  396.  
  397. // =======================================================
  398. // Returns number of TreeNode objects currently visible
  399. // =======================================================
  400. function numVisibleTreeNodes()
  401. {
  402.     var tNode;
  403.     var numVisible = 0;
  404.  
  405.     // Count visible TreeNodes (not counting root)
  406.     for (var i = 1; i < gTreeNodes.length; i++)
  407.     {
  408.         // Get the TreeNode
  409.         tNode = gTreeNodes[i];
  410.         if (!tNode.hidden)
  411.             numVisible++;
  412.     }
  413.     return numVisible;
  414. }
  415.  
  416. // ==============================================================
  417. // Called from GridView <html:canvas ...> mouse onclick event
  418. // Returns gTreeNodes[] TreeNode user clicked on, or null if none 
  419. // ==============================================================
  420. function gridViewTreeNodeFromMouseXY(x, y)
  421. {
  422.     // Return GridView ".jpeg" the user clicked on (if any)
  423.     var tNode;
  424.     for (var i = 1; i < gTreeNodes.length; i++)
  425.     {
  426.         // Only draw images for nodes that are visible in TreeView
  427.         tNode = gTreeNodes[i];
  428.         if (!tNode.hidden)
  429.         {
  430.             // Check if user clicked anywhere on GridView ".jpeg"
  431.             if (x > tNode.gridViewImgX
  432.              && x < tNode.gridViewImgX + tNode.gridViewImgWid 
  433.              && y > tNode.gridViewImgY
  434.              && y < tNode.gridViewImgY + (tNode.gridViewImgWid * WH_RATIO))
  435.             {
  436.                 // Return TreeNode that user clicked on
  437.                 return tNode;
  438.             }
  439.         }
  440.     }
  441.  
  442.     // The user did NOT click on ANY GridView ".jpeg"
  443.     return null;
  444. }
  445.